<template>
  <div>
    <van-pull-refresh v-model="isLoading" @refresh="onRefresh">
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
        :immediate-check="false"
      >
        <!-- 文章列表 -->
        <article-item
          v-for="myList in list"
          :key="myList.art_id"
          :listInfo="myList"
        >
        </article-item>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
import { reqArticleList } from "@/api";
import ArticleItem from "./ArticleItem";
export default {
  name: "ArticleList",
  data() {
    return {
      list: [],
      loading: false,
      finished: false,
      theTime: new Date().getDate(),
      isLoading: false,
    };
  },
  props: ["chanId"],
  components: {
    ArticleItem,
  },
  created() {
    this.getArticleList();
  },
  methods: {
    async getArticleList() {
      try {
        const res = await reqArticleList({
          channel_id: this.chanId,
          timestamp: this.theTime,
        });
        if (res.status == 200) {
          this.list = [...this.list, ...res.data.data.results];
          this.theTime = res.data.data.pre_timestamp;

          // 为了onLoad的调用
          this.loading = false;
          if (res.data.data.results.pre_timestamp === null) {
            this.finished = true;
          }
          // 为了onRefresh的调用
          this.isLoading = false;
        }
      } catch (error) {
        alert(error.message);
      }
    },
    async onLoad() {
      if (this.list.length === 0) {
        this.loading = false;
        return;
      }
      this.getArticleList();
    },
    async onRefresh() {
      this.list = [];
      this.theTime = new Date().getDate();
      this.getArticleList();
    },
  },
};
</script>
